package com.oyb.filter;

import com.oyb.entity.Admin;
import com.oyb.util.PropertiesUtils;


import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebFilter(urlPatterns = "/*")
public class Filter100login implements Filter {

    //初始化
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }
//过滤逻辑触发
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest) servletRequest;
        HttpServletResponse resp = (HttpServletResponse) servletResponse;
        String uri = req.getRequestURI();//获得拦截的url
        Admin admin = (Admin) req.getSession().getAttribute("login_user");
        if(admin != null){
            filterChain.doFilter(req,resp);
        }else{
            String nologin = PropertiesUtils.getProperties("nologin");
            if(nologin != null){
                String[] res = nologin.split("\\|");
                for(String re : res){
                    if (uri.endsWith(re)){
                        filterChain.doFilter(req,resp);
                        return;
                    }
                }
            }
            resp.sendRedirect("login.jsp");
        }

    }
//销毁
    @Override
    public void destroy() {

    }
}
